package org.javaboy.demo.service;

import com.alibaba.druid.mock.MockSavepoint;
import com.mysql.cj.jdbc.MysqlSavepoint;
import org.javaboy.demo.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Savepoint;

@Repository
public class UserService {

    @Autowired
    UserDao userDao;
    @Autowired
    UserService2 us2;

    @Transactional(rollbackFor = Exception.class,readOnly = true,timeout = 60000)
    public void update1() throws IOException {
        userDao.minusMoney("lisi", 100.0);
        us2.update2();
//        int i = 1 / 0;
//        throw new IOException();
    }

//    public void test() {
//        Connection con = null;
//        try {
//            con.setAutoCommit(false);
//            //xxx
//            //xxx
//            //设置保存点
//            con.setSavepoint("a");
//            //xxx
//            //xxx
//            con.setSavepoint("b");
//            //xx
//            //xx
//            con.commit();
//        } catch (SQLException e) {
//            try {
//                con.rollback(new MysqlSavepoint());
//                con.rollback();
//            } catch (SQLException ex) {
//                throw new RuntimeException(ex);
//            }
//            throw new RuntimeException(e);
//        }
//    }
}
